package com.hhit.bigscreenbackmanage.service.impl.aqgl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hhit.bigscreenbackmanage.dao.aqgl.AqglFxCountDao;
import com.hhit.bigscreenbackmanage.entity.aqgl.TAqglFxCount;
import com.hhit.bigscreenbackmanage.service.aqgl.AqglFxCountService;
import com.hhit.bigscreenbackmanage.util.DateUtils;
import com.hhit.bigscreenbackmanage.util.Utils;
import com.hhit.bigscreenbackmanage.vo.aqgl.AqglSearchVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName AqglFxCountServiceImpl
 * @Description
 * @Author qianchengcheng
 * @Date 2021/7/21
 */
@Service
public class AqglFxCountServiceImpl implements AqglFxCountService {

    private static Logger log = LoggerFactory.getLogger(AqglFxCountServiceImpl.class);

    @Resource
    private AqglFxCountDao aqglFxCountDao;

    @Override
    public Map<String, Object> importAgqlFxCount(String propName, MultipartFile[] files,String datetime) throws Exception {
        log.info("AqglFxCountServiceImpl.importAgqlFxCount Call Begin,param[propName:{},files:{},datetime:{}]",propName,files,datetime);
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            if (StrUtil.isNotEmpty(datetime)){
                List<String> ids = aqglFxCountDao.getAqglFxCountByDateTime(datetime);
                if (CollectionUtil.isNotEmpty(ids)){
                    int delRes = aqglFxCountDao.batchDeleteAqglFxCountByIds(ids);
                    log.info("AqglFxCountServiceImpl.importAgqlFxCount==>batchDeleteAqglFxCountByIds res:{}",delRes);
                }
            }
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TAqglFxCount> list = (List<TAqglFxCount>)Utils.mapToEntiry(result, TAqglFxCount.class);
                List<TAqglFxCount> filterList = list.stream().filter(item
                        -> !StringUtils.isEmpty(item.getDw())).collect(Collectors.toList());
                if(filterList.size() > 0 ){
                    //处理UUID
                    filterList.forEach(item->{
                        item.setId(IdUtil.fastSimpleUUID());
                        item.setDatetime(datetime);
                        item.setYjfxzs("0");
                        item.setYjfxdgdwl("100%");
                    });
                    int count =  aqglFxCountDao.batchInsertAqglFxCount(filterList);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public PageInfo getAqglFxCountPageInfo(AqglSearchVo aqglSearchVo) throws Exception {
        log.info("AqglFxCountServiceImpl.getAqglFxCountPageInfo Call Begin,param[aqglFxCountSearchVo:{}]", aqglSearchVo);
        int pageNum = aqglSearchVo.getPageNum() == null ? 1 : aqglSearchVo.getPageNum().intValue();
        int pageSize = aqglSearchVo.getPageSize() == null ? 15 : aqglSearchVo.getPageSize().intValue();
        PageHelper.startPage(pageNum, pageSize);
        List<TAqglFxCount> tAqglFxCountList = aqglFxCountDao.getAqglFxCountPageInfo(aqglSearchVo);
        PageInfo<TAqglFxCount> pageInfo = new PageInfo<>(tAqglFxCountList);
        return pageInfo;
    }

    @Override
    public TAqglFxCount getAqglFxCountEntityByParam(String cityName) throws Exception {
        log.info("AqglFxCountServiceImpl.getAqglFxCountEntityByParam Call Begin,param[cityName:{}]",cityName);
        if (StrUtil.isEmpty(cityName)){
            cityName = "全省";
        }
        Map<String,Object> param = new HashMap<>();
        param.put("cityName",cityName);
        param.put("datetime", DateUtils.formateDate(new Date()));
        return aqglFxCountDao.getAqglFxCountEntityByParam(param);
    }

    @Override
    public List<TAqglFxCount> getAqglFxCounts(String datetime, String cityName) throws Exception {
        List<TAqglFxCount> aqglFxCountList = new ArrayList<>();
        if (StrUtil.isEmpty(datetime)){
            cityName = "全省";
        }
        aqglFxCountList = aqglFxCountDao.getAqglFxCounts(datetime,cityName);
        return aqglFxCountList;
    }

    @Override
    public TAqglFxCount getAqglFxCountRecently(String datetime, String cityName) throws Exception {
        if (StrUtil.isEmpty(cityName)){
            cityName = "全省";
        }
        return aqglFxCountDao.getAqglFxCountRecently(datetime,cityName);
    }
}
